//package yb.ecp.fast.infra.nacos;
//
//import cn.hutool.core.collection.CollUtil;
//import com.alibaba.cloud.nacos.NacosConfigProperties;
//import com.alibaba.nacos.api.NacosFactory;
//import com.alibaba.nacos.api.config.ConfigService;
//import com.alibaba.nacos.api.config.listener.Listener;
//import com.alibaba.nacos.api.exception.NacosException;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
//import org.springframework.cloud.gateway.event.RefreshRoutesEvent;
//import org.springframework.cloud.gateway.route.RouteDefinition;
//import org.springframework.cloud.gateway.route.RouteDefinitionWriter;
//import org.springframework.context.ApplicationEventPublisher;
//import org.springframework.context.ApplicationEventPublisherAware;
//import org.springframework.context.annotation.Configuration;
//import reactor.core.publisher.Mono;
//import yb.ecp.fast.infra.infra.FastJsonUtil;
//
//import javax.annotation.PostConstruct;
//import java.util.ArrayList;
//import java.util.Collections;
//import java.util.List;
//import java.util.concurrent.Executor;
//
//@Slf4j
//@ConditionalOnBean(NacosConfigProperties.class)
//@Configuration
//public class DynamicRouteConfiguration implements ApplicationEventPublisherAware {
//
//    @Autowired
//    private NacosConfigProperties nacosConfigProperties;
//
//    @Value("${spring.cloud.gateway.dynamic.data-id:gateway-route}")
//    private String dataId;
//
//    @Autowired
//    private RouteDefinitionWriter routeDefinitionWriter;
//
//    private ApplicationEventPublisher applicationEventPublisher;
//
//    //用于记录加载的配置文件
//    private static final List<String> ROUTE_LIST = Collections.synchronizedList(new ArrayList<>());
//
//    @PostConstruct
//    public void dynamicRouteByNacosListener() {
//        try {
//
//            ConfigService configService = NacosFactory.createConfigService(nacosConfigProperties.assembleConfigServiceProperties());
//            // 程序首次启动, 并加载初始化路由配置
//            String initConfigInfo = configService.getConfig(dataId, nacosConfigProperties.getGroup(), 5000);
//
//            this.addRouteConfig(initConfigInfo);
//
//            //添加监听器监听nacos配置文件内的路由变化
//            configService.addListener(dataId, nacosConfigProperties.getGroup(), new Listener() {
//                @Override
//                public void receiveConfigInfo(String configInfo) {
//                    refreshRouteConfig(configInfo);
//                }
//
//                @Override
//                public Executor getExecutor() {
//                    return null;
//                }
//            });
//        } catch (NacosException e) {
//            log.error("", e);
//        }
//    }
//
//
//    private void publish() {
//        this.applicationEventPublisher.publishEvent(new RefreshRoutesEvent(this.routeDefinitionWriter));
//    }
//
//    @Override
//    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
//        this.applicationEventPublisher = applicationEventPublisher;
//    }
//
//    /**
//     * 刷新路由配置
//     *
//     * @param configInfo 配置文件字符串, 必须为json array格式
//     */
//    private void refreshRouteConfig(String configInfo) {
//        clearRoute();
//        addRouteConfig(configInfo);
//    }
//
//    private void addRouteConfig(String configInfo) {
//        try {
//            //将yaml转为java对象
//            GatewayRouteConfigProperties gatewayRouteConfig = YamlUtil.toBean(configInfo, GatewayRouteConfigProperties.class);
//
//            if (gatewayRouteConfig != null && CollUtil.isNotEmpty(gatewayRouteConfig.getRouteDefinition())) {
//                gatewayRouteConfig.getRouteDefinition().parallelStream().forEach(this::addRoute);
//            }
//
//            publish();
//            log.info("动态配置路由加载完成 {}", FastJsonUtil.toJSONString(gatewayRouteConfig));
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//
//    /**
//     * 清除路由信息
//     */
//    private void clearRoute() {
//        ROUTE_LIST.stream().forEach(id -> {
//            this.routeDefinitionWriter.delete(Mono.just(id)).subscribe();
//        });
//        ROUTE_LIST.clear();
//    }
//
//    /**
//     * 添加路由
//     */
//    private void addRoute(RouteDefinition definition) {
//        try {
//            routeDefinitionWriter.save(Mono.just(definition)).subscribe();
//            ROUTE_LIST.add(definition.getId());
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//
//}
